#include "VStdAfx.h"
#include "VSceneEntity.h"
#include "VScene.h"
#include "KEntityAgentSceneInfo.h"
#include "KScene.h"
#include "KWorld.h"

VSceneEntity::VSceneEntity(void)
	:m_SceneNodeInfo(NULL)
	,m_MaxVisibleDistance(0.0f)
	,m_Visible(1)
	,m_CastShadow(1)
	,m_AgentStateDirty(0)
	,m_AgentTransformDirty(0)
	,m_RenderReference(0)
{
	
}

VSceneEntity::~VSceneEntity(void)
{
}

void VSceneEntity::OnTransformDirty()
{
	m_WorldTransformDirty = 1;
	m_WolrdMatrixDirty = 1;
	m_AgentTransformDirty = 1;
	NeedFlushSceneAgent();
}

void VSceneEntity::AddToScene(VScene* Scene)
{
	VASSERT(m_Scene == NULL);
	VASSERT(Scene);


}

void VSceneEntity::RemoveFromScene()
{
	VASSERT(m_Scene);

	if (m_Scene)
	{
		m_Scene->RemoveComponent(this);
	}
}

void VSceneEntity::Update(float DeltaTime)
{
	// Update Controllers

	if (m_AgentTransformDirty)
	{

		m_AgentTransformDirty = 0;
	}

	if (m_AgentStateDirty)
	{
		// upload agent state 
		m_AgentStateDirty = 0;
	}
}

void VSceneEntity::PostUpdate()
{

}

void VSceneEntity::SetVisible(BOOL Visible)
{
	if (m_Visible != Visible)
	{
		m_Visible = Visible;
		m_AgentStateDirty = 1;
		NeedFlushSceneAgent();
	}
}

void VSceneEntity::UpdateBound()
{

}

void VSceneEntity::UpdateWorldMatrix() const
{
	const VTransform& WorldTransform = GetWorldTransform();
	WorldTransform.ToMatrix(m_WorldMatrix);
	m_WolrdMatrixDirty = 0;
}

VSceneEntityAgent* VSceneEntity::CreateSceneAgent()
{
	return NULL;
}

BOOL VSceneEntity::Load(const VXMLElement* Element)
{
	if (!VSceneObject::Load(Element))
	{
		return FALSE;
	}

	const VXMLElement* XSceneEntity = Element->FirstChildElement("SceneEntity");
	if (XSceneEntity)
	{
		m_Visible = VXML_LoadBool(XSceneEntity, "Visible", TRUE);
		m_CastShadow = VXML_LoadBool(XSceneEntity, "CastShadow", TRUE);
		m_MaxVisibleDistance = VXML_LoadFloat(XSceneEntity, "VisibleDistance", 0.0f);
	}

	UpdateWorldMatrix();
	

	return TRUE;
}

void VSceneEntity::Save(VXMLElement* Element)
{

}




//////////////////////////////////////////////////////////////////////////

void VSceneEntityAgent::RT_SetShaderParameters(EVRenderPass Pass, const VMaterialShaderParameters* Parameters, VShader* Shader, const VRenderSurface* Surface)
{

}